﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Reflection;
using ClassLibrary1;
using System.Drawing.Imaging;
namespace CKNetFrameWork
{
    public partial class Frm_Main : Form
    {
        private Frm_MainToolWindow mainToolWin = null;
        private static Frm_Main m_Frm_Main = null;
        private Dictionary<string, string> m_dicFormText2FormTypeName = new Dictionary<string, string>();
        public static int m_nLoginUserId = 0;
        public Frm_Main()
        {
            InitializeComponent();
        }
        private void InitFormText2FormTypeNameDic()
        {
            m_dicFormText2FormTypeName.Add("产品管理", "CKNetFrameWork.Frm_Product");
            m_dicFormText2FormTypeName.Add("区域管理", "CKNetFrameWork.Frm_Region");
            m_dicFormText2FormTypeName.Add("代理商管理", "CKNetFrameWork.Frm_Agent");
            m_dicFormText2FormTypeName.Add("零售商管理", "CKNetFrameWork.Frm_Seller");

            m_dicFormText2FormTypeName.Add("订单管理", "CKNetFrameWork.Frm_Order");
            

            m_dicFormText2FormTypeName.Add("充值管理", "CKNetFrameWork.Frm_Deposit");
            m_dicFormText2FormTypeName.Add("入库管理", "CKNetFrameWork.Frm_InStore");
            m_dicFormText2FormTypeName.Add("数字小写转大写", "ClassLibrary1.Form1");
            
        }
        public static Frm_Main GetMainForm()
        {
            if (m_Frm_Main == null)
            {
                m_Frm_Main = new Frm_Main();
            }

            return m_Frm_Main;
        }
        private DockContent FindDocument(string text)
        {
            if (dockPanel.DocumentStyle == DocumentStyle.DockingMdi)
            {
                foreach (Form form in MdiChildren)
                {
                    if (form.Text == text)
                    {
                        return form as DockContent;
                    }
                }

                return null;
            }
            else
            {
                foreach (DockContent content in dockPanel.Documents)
                {
                    if (content.DockHandler.TabText == text)
                    {
                        return content;
                    }
                }

                return null;
            }
        }

        public void ShowContent(DockContent frm)
        {
            frm.Show(this.dockPanel);
            frm.Focus();
            frm.BringToFront();
        }

        public DockContent ShowContent(string caption/*, Type formType*/)
        {

            DockContent frm = FindDocument(caption);

            if (frm == null)
            {
                string strTemp = m_dicFormText2FormTypeName[caption];
                string[] strSplit = strTemp.Split('.');
                string strAssemblyName = strSplit[0];

                //Assembly assembly = Assembly.GetExecutingAssembly();
                Assembly assembly = Assembly.Load(strAssemblyName);
                //DockContent currentMdiChild = null;
                
                if (!m_dicFormText2FormTypeName.ContainsKey(caption))
                    return frm;
                Type typForm = assembly.GetType(m_dicFormText2FormTypeName[caption]);

                //关于InvokeMember用法，不明白的可以查看：http://msdn.microsoft.com/zh-cn/library/de3dhzwy(VS.80).aspx
                Object mdiChild = typForm.InvokeMember(
                   null,
                    BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,
                   null,
                   null,
                   null);

                if (mdiChild != null)
                {
                    frm = mdiChild as DockContent;
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;

                }
            }

            frm.Show(this.dockPanel);
            frm.Focus();
            frm.BringToFront();
            return frm;
        }
        private void Frm_Main_Load(object sender, EventArgs e)
        {
           
            //Frm_Login frm_login = new Frm_Login();

            //if (frm_login.ShowDialog() == DialogResult.OK)
            //{
            //    int a = 0;
            //}
            //else
            //{
            //    int b = 0;
            //    Application.Exit();
            //    return;
            //}
            mainToolWin = new Frm_MainToolWindow();
            mainToolWin.Show(this.dockPanel, DockState.DockLeft);
            InitFormText2FormTypeNameDic();

            this.dockPanel.DockLeftPortion = 0.17;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ScreenCapture sc = new ScreenCapture();
            sc.CaptureWindowToFile(this.Handle, "d:/a.jpg", ImageFormat.Jpeg);
        }
    }
}
